﻿Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.Text
Imports System.Text.RegularExpressions

Public Class Graph
    Inherits PropertyChangedBase

    Private _Nodes As ObservableCollection(Of Node)
    Public Property Nodes As ObservableCollection(Of Node)
        Get
            Return _Nodes
        End Get
        Set(value As ObservableCollection(Of Node))
            _Nodes = value
            OnPropertyChanged("Nodes")
        End Set
    End Property

    Private _Edges As ObservableCollection(Of Edge)
    Public Property Edges As ObservableCollection(Of Edge)
        Get
            Return _Edges
        End Get
        Set(value As ObservableCollection(Of Edge))
            _Edges = value
            OnPropertyChanged("Edges")
        End Set
    End Property

    Private _Name As String
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(value As String)
            _Name = value
            OnPropertyChanged("Name")
        End Set
    End Property

    Private _FileType As String
    Public Property FileType As String
        Get
            Return _FileType
        End Get
        Set(value As String)
            _FileType = value
            OnPropertyChanged("FileType")
        End Set
    End Property

    Public Sub New()
        Me.Edges = New ObservableCollection(Of Edge)
        Me.Nodes = New ObservableCollection(Of Node)
    End Sub

    Public Shared ReadOnly NodeRegex = New Regex("\A(((""(?<name>[^""]+)"")|(?<name>[^""\s]+))\s*\[\s*image\s*=\s*""(?<image>[^""]+)"".+?\])[^;]*;", RegexOptions.Compiled)
    Public Shared ReadOnly EdgeRegex = New Regex("", RegexOptions.Compiled)
    Public Sub New(filepath As String)
        'TODO:读取输入文件，转化为对象。
        Dim r = My.Computer.FileSystem.OpenTextFileReader(filepath)

    End Sub

    Public Overrides Function ToString() As String
        Dim sb As New StringBuilder("digraph ")
        sb.AppendFormat("{0} {{{1}charset = ""latin1"";{1}label = ""{0}"";edge [font=""Microsoft YaHei""];{1}", Name, Environment.NewLine)
        For Each nd In Nodes
            sb.AppendLine(nd.ToString)
        Next
        sb.AppendLine()
        For Each eg In Edges
            sb.AppendLine(eg.ToString)
        Next
        sb.AppendLine("}")
        Return sb.ToString
    End Function
End Class
